var options = {
    // 初始化选项
};
$('#myUploader').uploader(options);
upfile();
function upfile(){
    var uploader = $('#myUploader').data('zui.uploader');
    console.log(uploader);
}


$('#picker3').picker();
function initPage(pageNo,totalCount,arr){
    // 获取分页器实例对象
    var myPager = $('#myPager').data('zui.pager');
    $('#subjectCount')[0].innerHTML = '共' + totalCount + '题'
        //设置当前页码为 4，并同时设置记录总数为 100， 每页记录数目为 30
    myPager.set(pageNo, totalCount, 5);
    renderSubscribeList(arr);
}
$('#myPager').pager({
    onPageChange: function(state, oldState) {
        if (state.page !== oldState.page) {
            // console.log('页码从', oldState.page, '变更为', state.page);
            queryBtn(state.page);
        }
    }
});
getFieldList();
var fieldArr=[]
function getFieldList(){
    $.ajax({
        url:config.url + "/getFieldList",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        type:'GET' ,
        success:function(resp) {
            addOptionList(resp.data);
            queryBtn(1);
            fieldArr = resp.data;
        },
        error:function() {
        }
    });
}
function addOptionList(arr){
    let str = '';
    for (let i = 0; i < arr.length; i++) {
        let a  = arr[i];
        str += `<option value="${a.id}" >${a.fieldName}</option>`;
    }
    $('#picker666')[0].innerHTML = str;
}
function chooseSubject(){
    console.log();

    queryBtn(1);
}
function queryBtn(i){
    $('#picker666').val();
    $.ajax({
        url:config.url + "/subject/getList",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            pageNo:i,
            fieldId:$('#picker666').val()
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            var totalCount = resp.data.totalCount;
            initPage(resp.pageNo,totalCount,resp.data.list);
        },
        error:function() {
        }
    });
}
function renderSubscribeList(arr){
    let str = '';
    for (let i = 0; i < arr.length; i++) {
        let a  = arr[i];
        str += `<tr>
                <td>${a.name}</td>
                <td>${a.option1}</td>
                <td>${a.option2}</td>
                <td>${a.option3}</td>
                <td>${a.option4}</td>
                <td  style="text-align: left">
                    <button onclick="updateBtn(${a.colNum},${a.colNum})"
                    type="button" data-toggle="modal" data-target="#myModal2" 
                    id="updateBtn"  
                    class="btn btn-info updateBtn btn-primary">
                    修改</button>
                    <button onclick="delBtn(${a.colNum})" 
                    data-toggle="modal" data-target="#myModal3" 
                    id="delBtn" 
                    class="delBtn btn btn-danger btn-primary" >
                    删除</button>
                </td>
            </tr>`;
    }
    $('#tbody')[0].innerHTML = str;
}
let data;
function updateBtn(colNum){
    console.log(colNum)
    $.ajax({
        url:config.url + "/subject/getOneSubject",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            colNum:colNum,
            fieldId:$('#picker666').val()
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            console.log(resp.data);
            data = resp.data;
            $('#field').val(data.fieldName);
            $('#num').val(data.colNum);
            $('#subject').val(data.name);
            $('#o1').val(data.option1);
            $('#o2').val(data.option2);
            $('#o3').val(data.option3);
            $('#o4').val(data.option4);
        },
        error:function() {
        }
    });
}
var colNumGlobal;
function delBtn(colNum){
    colNumGlobal = colNum;
}
function submitSubjectForm(){
    let fieldName = $('#field').val();
    let colNum = $('#num').val();
    let name = $('#subject').val();
    let o1 = $('#o1').val();
    let o2 = $('#o2').val();
    let o3 = $('#o3').val();
    let o4 = $('#o4').val();
    $.ajax({
        url:config.url + "/subject/updateOneSubject",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            fieldName:fieldName,
            colNum:colNum,
            name:name,
            option1:o1,
            option2:o2,
            option3:o3,
            option4:o4
        },
        crossDomain: true,
        type:'POST',
        success:function(resp) {
            alert(resp.msg);
            location.reload();
        },
        error:function() {
        }
    });
}
function submitDelForm(){
    console.log(colNumGlobal);
    console.log($('#picker666').val());
    $.ajax({
        url:config.url + "/subject/deleteOneSubject",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            fieldId:$('#picker666').val(),
            colNum:colNumGlobal
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            alert(resp.msg);
            location.reload();
        },
        error:function() {
        }
    });
}
function addOneSubject(){
    let fieldId = $('#picker666').val();
    let fieldName = '';
    if (fieldId == 1){
        fieldName = '亲子关系';
    }else if(fieldId == 2){
        fieldName = '婚姻关系';
    }else if(fieldId == 3){
        fieldName = '情绪管理';
    }else if(fieldId == 4){
        fieldName = '人际关系';
    }
    else if(fieldId == 5){
        fieldName = '家庭处理';
    }
    $('#field1').val(fieldName);
    console.log(fieldId);

    let name = $('#subject1').val();
    let o1 = $('#o11').val();
    let o2 = $('#o22').val();
    let o3 = $('#o33').val();
    let o4 = $('#o44').val();
    $.ajax({
        url:config.url + "/subject/addOneSubject",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            fieldId:fieldId,
            name:name,
            option1:o1,
            option2:o2,
            option3:o3,
            option4:o4
        },
        crossDomain: true,
        type:'POST',
        success:function(resp) {
            alert(resp.msg);
            location.reload();
        },
        error:function() {
        }
    });

}